package com.example.mybatisplusdemo.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplusdemo.common.utls.SessionUtils;
import com.example.mybatisplusdemo.model.dto.LocationDTO;
import com.example.mybatisplusdemo.model.dto.PageDTO;
import com.example.mybatisplusdemo.service.IFavoriteService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.mybatisplusdemo.common.JsonResponse;
import com.example.mybatisplusdemo.service.IStoreService;
import com.example.mybatisplusdemo.model.domain.Store;

import java.util.List;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-07-04
 * @version v1.0
 */
@RestController
@RequestMapping("/api/store")
public class StoreController {

    private final Logger logger = LoggerFactory.getLogger( StoreController.class );

    @Autowired
    private IFavoriteService favoriteService;


    @Autowired
    private IStoreService storeService;

    @GetMapping("getById")
    public JsonResponse getById(Long id) {
        Store store = storeService.getStoreById(id);
        return JsonResponse.success(store);
    }

    //根据主类别筛选店铺
    @GetMapping("getStoresByCategory")
    public JsonResponse getStoresByCategory(String category) {
        List<Store> stores = storeService.getStoresByCategory(category);
        return JsonResponse.success(stores);
    }

    //选取收藏数最高的20个店铺
    @GetMapping("top20FavoriteStores")
    public JsonResponse topFavoriteStores() {
        List<Store> stores = storeService.getTop20FavoriteStores();
        return JsonResponse.success(stores);
    }

    //根据店铺ID获取收藏数
    @GetMapping("getFavoriteCountById")
    public JsonResponse getFavoriteCountById(Long id) {
        Long favoriteCount = storeService.getFavoriteCountById(id);
        return JsonResponse.success(favoriteCount);
    }

    //根据店铺ID获取评论数
    @GetMapping("getReviewCountById")
    public JsonResponse getReviewCountById(Long id) {
        Long reviewCount = storeService.getReviewCountById(id);
        return JsonResponse.success(reviewCount);
    }

    //根据店铺ID计算平均分
    @GetMapping("getAvgScoreById")
    public JsonResponse getAvgScoreById(Long id) {
        Double avgScore = storeService.getAvgScoreById(id);
        //取一位小数
        avgScore = Math.round(avgScore * 10.0) / 10.0;
        return JsonResponse.success(avgScore);
    }

    //获取当前店铺信息
    @GetMapping("getInfo")
    public JsonResponse getStoreInfo(Long id) {
        Store currentStoreInfo = SessionUtils.getCurrentStoreInfo();
        return JsonResponse.success(currentStoreInfo);
    }

    //插入店铺
    @PostMapping("/insertStore")
    public JsonResponse insertStore(@RequestBody Store store) {
        boolean b = storeService.save(store);
        return JsonResponse.success(b);
    }

    //更新店铺
    @PostMapping("/updateStoreById")
    public JsonResponse updateStoreById(@RequestBody Store store) {
        boolean result = storeService.updateStore(store);
        return JsonResponse.success(result);
    }

    //删除店铺
    @GetMapping("/deleteById")
    public JsonResponse deleteStoreById(Long id) {
        boolean b = storeService.removeById(id);
        return JsonResponse.success(b);
    }

    //关停店铺
    @GetMapping("/closeStoreById")
    public JsonResponse closeStoreById(Long id) {
        boolean b = storeService.closeStoreById(id);
        return JsonResponse.success(b);
    }

    //查询店铺
    @GetMapping("/getListByMerchantId")
    public JsonResponse getStoreListByMerchantId(Long merchantId) {
        return JsonResponse.success(storeService.getStoreListByMerchantId(merchantId));
    }

    //搜索功能
    @GetMapping("/search")
    public List<Store> searchStores(String keyword) {
        return storeService.searchStores(keyword);
    }

    // 5星评分前20的店铺
    @GetMapping("/top20FiveStarStores")
    public JsonResponse top20FiveStarStores() {
        List<Store> stores = storeService.getTop20FiveStarStores();
        return JsonResponse.success(stores);
    }

    // 根据店铺ID获取5星评分的数量
    @GetMapping("/getFiveStarCountById")
    public JsonResponse getFiveStarCountById(Long id) {
        Long fiveStarCount = storeService.getFiveStarCountById(id);
        return JsonResponse.success(fiveStarCount);
    }

    // 评论数前20的店铺
    @GetMapping("/top20ReviewStores")
    public JsonResponse top20ReviewStores() {
        List<Store> stores = storeService.getTop20ReviewStores();
        return JsonResponse.success(stores);
    }

    // 平均分前10的店铺
    @GetMapping("/top10AvgScoreStores")
    public JsonResponse top10AvgScoreStores() {
        List<Store> stores = storeService.getTop10AvgScoreStores();
        return JsonResponse.success(stores);
    }

    // 获取待审核店铺列表
    @GetMapping("/listPendingStores")
    public JsonResponse listPendingStores(PageDTO pageDTO) {
        Page<Store> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
        page = storeService.getPendingStores(page);
        return JsonResponse.success(page);
    }

    //根据用户id查询其收藏的所有店铺
    @GetMapping("/getFavoriteShopsById")
    public JsonResponse getFavoriteShopsById(Long id) {
        //调用Service获取收藏店铺列表
        List<Store> stores = favoriteService.getFavoriteShopsById(id);
        return JsonResponse.success(stores);
    }

    //根据店铺id查询其相应的评分
    @GetMapping("/getCurStoreRatingByStoreId")
    public double getCurStoreRatingByStoreId(Long storeId) {
        double rating = storeService.getStoreRating(storeId);
        return rating;
    }

    // 店铺审核通过
    @GetMapping("approveStore")
    public JsonResponse approveStore(Long storeId) {
        boolean result = storeService.approveStore(storeId);
        return JsonResponse.success(result);
    }

    @GetMapping("rejectStore")
    public JsonResponse rejectStore(Long storeId) {
        boolean result = storeService.rejectStore(storeId);
        return JsonResponse.success(result);
    }

    // 提取店铺经纬度信息
    @GetMapping("getStoreLocationById")
    public JsonResponse getStoreLocation(Long storeId) {
        LocationDTO locationDTO = storeService.getStoreLocationById(storeId);
        return JsonResponse.success(locationDTO);
    }

    //计算两个位置之间的距离
    @GetMapping("calculateDistance")
    public JsonResponse calculateDistance(Double lon1, Double lat1, Double lon2, Double lat2) {
        Double distance = storeService.calculateDistance(lon1, lat1, lon2,lat2);
        return JsonResponse.success(distance);
    }
}